package com.apiit.webserviceclient.activities;

import java.util.HashMap;
import java.util.Map;

import com.apiit.webserviceclient.R;
import com.apiit.webserviceclient.R.layout;
import com.apiit.webserviceclient.R.menu;
import com.apiit.webserviceclient.utility.BPEL_Process;
import com.apiit.webserviceclient.utility.BPEL_Request;
import com.apiit.webserviceclient.utility.BPEL_Response;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class LibraryRenewals extends Activity implements OnClickListener {
	
	TextView patronNo;
	TextView name;
	TextView status;
	TextView expDate;
	TextView message;
	Button history;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_library_renewals);
		
		patronNo = (TextView) findViewById(R.id.txtRenPatrron);
		name = (TextView) findViewById(R.id.txtRenName);
		status = (TextView) findViewById(R.id.txtenStatus);
		expDate = (TextView) findViewById(R.id.txtRenexp);
		message = (TextView) findViewById(R.id.RenewalStatus);
		history = (Button) findViewById(R.id.btnLoanHistory);
		history.setOnClickListener(this);
		
		Intent intename = getIntent();
		String patronNoValue = intename.getStringExtra("patronNo");
		String nameValue = intename.getStringExtra("Name");
		String statusValue = intename.getStringExtra("Status");
		String expDateValue = intename.getStringExtra("expDate");
		String messageValue = intename.getStringExtra("message");
		

		patronNo.setText(patronNoValue);
		name.setText(nameValue);
		status.setText(statusValue);
		expDate.setText(expDateValue);
		
		//message.setText(messageValue);
		if(messageValue.equalsIgnoreCase("&nbsp;")){
			
			BPEL_Process bpelProcess = new BPEL_Process();
			BPEL_Request request = new BPEL_Request();
			request.setUrl("http://webspace1.apiit.lk/gims/library/LoanItemsMain.asp");
			Map<String, String> xpathMap = new HashMap<String, String>();
			xpathMap.put("table","/html[1]/body[1]/table[2]---");

			request.setXpathMap(xpathMap);
			Map<String, String> parametersMap = new HashMap<String, String>();
			request.setParametersMap(parametersMap);
			request.setType("GET");
			BPEL_Response response = null;
			try {
				response = bpelProcess.doRequest(request);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			//String form = "<form method="POST" action="/gims/library/LoanItemsMain.asp">";
			String table = response.getResultsMap().get("table").replaceAll("Title", "").replaceAll("Item Code", "").replaceAll("Date Borrowed", "").replaceAll("Rent Due Date", "").replaceAll("&nbsp", "").replaceAll("/t", "").replaceAll("/n", "");
			String result [] = table.split(";");
			
			Toast.makeText(getApplicationContext(), result[5],
					Toast.LENGTH_LONG).show();
			Intent i = new Intent(this,IsBookBorrowed.class);
			i.putExtra("array",result);
			startActivity(i);
		}
		else
		{
			message.setText(messageValue);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.library_renewals, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v == history) {
			BPEL_Process bpelProcess = new BPEL_Process();
			BPEL_Request request = new BPEL_Request();
			request.setUrl("http://webspace1.apiit.lk/gims/library/LoanItemsMain.asp");
			Map<String, String> xpathMap = new HashMap<String, String>();
			xpathMap.put("message", "/html[1]/body[1]/table[2]---");
			request.setXpathMap(xpathMap);

			
			//action=ShowLoanHistory&submit=Show+Loan+History
			Map<String, String> parametersMap = new HashMap<String, String>();
			parametersMap.put("submit", "Show+Loan+History");
			parametersMap.put("action", "ShowLoanHistory");
			request.setParametersMap(parametersMap);

			request.setType("POST");
			BPEL_Response respones = null;
			try {
				respones = bpelProcess.doRequest(request);

			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			String testResult = respones.getResultsMap().get("message").replaceAll("Item Code", "").replaceAll("Title", "").replaceAll("Date Borrowed", "").replaceAll("Date Returned", "").replaceAll("&nbsp", "").replaceAll("\t", "").replaceAll("\n", "");
			String result [] = testResult.split(";");
			Toast.makeText(getApplicationContext(), testResult,
					Toast.LENGTH_LONG).show();
			
			Intent i = new Intent(this, LibraryLoans.class);
			i.putExtra("array",result);
			startActivity(i);
		}
		
	}

}
